package com.dx.mes.service.warehouse.service;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.bean.erp.ERSVXFR;
import com.dx.mes.common.bean.erp.EWOMTRL;
import com.dx.mes.common.core.rabbitmq.BaseConnFactory;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.facade.warehouse.service.IMwpmtrlopeService;
import com.dx.mes.facade.warehouse.tx.Mwpmtrlope.MwpmtrlopeI;
import com.dx.mes.facade.warehouse.tx.Mwpmtrlope.MwpmtrlopeIA;
import com.dx.mes.facade.warehouse.tx.Mwpmtrlope.MwpmtrlopeO;
import com.dx.mes.facade.warehouse.tx.Mwpmtrlope.MwpmtrlopeOA;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

/**
 * Created by admin on 2017/11/12.
 */
@Service("mwpmtrlopeService")
public class MwpmtrlopeService implements IMwpmtrlopeService {
    private LogUtils logUtils;
    private static String TX_NAME = T_MWPMTROPE;

    @Autowired
    private IBisMtrlDao bisMtrlDao;
    @Autowired
    private IRetWoMtrlDao retWoMtrlDao;
    @Autowired
    private IRetWhMtrlDao retWhMtrlDao;
    @Autowired
    private IRetWoBomDao  retWoBomDao;
    @Autowired
    private IRetWhRsvDao  retWhRsvDao;
    @Autowired
    private IBisDataDao   bisDataDao;
    @Resource
    private BaseConnFactory baseConnFactory;



    @Override
    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(MwpmtrlopeService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(MwpmtrlopeService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        Session session = retWhMtrlDao.getCrSession();
        long rtnCode = _NORMAL;
        String outMsg = null;

        MwpmtrlopeI inTrx = new  MwpmtrlopeI();
        MwpmtrlopeO outTrx = new MwpmtrlopeO();
        outTrx.setTrx_id(TX_NAME);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
        outTrx.setType_id(TRX_OUT);
        try {
            inTrx = JacksonUtil.fromJson(strInMsg, MwpmtrlopeI.class);

            rtnCode = subMainProc2(inTrx, outTrx);
            if (rtnCode != _NORMAL) {
                TransactionAspectSupport.currentTransactionStatus()
                        .setRollbackOnly();
                outTrx.setRtn_code(String.valueOf(rtnCode));
            } else {
                outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
                outTrx.setRtn_mesg(RETURN_MESG_OK);
                session.flush();
            }
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outMsg + "]");
        }
        return outMsg;
    }

    private long subMainProc2(MwpmtrlopeI inTrx, MwpmtrlopeO outTrx) {
        long lRc = _NORMAL;
        char actionType = inTrx.getAction_flg().toCharArray()[0];
        switch (actionType) {
            case 'Q': {
                lRc = queryFunc(inTrx, outTrx);//库存查询
                if (lRc != _NORMAL) {
                    return lRc;
                }
                break;
            }
            case 'S': {
                lRc = saveFunc(inTrx, outTrx);//库存登记
                if (lRc != _NORMAL) {
                    return lRc;
                }
                break;
            }
            case 'D': {
                lRc = delFunc(inTrx, outTrx);//库存删除
                if (lRc != _NORMAL) {
                    return lRc;
                }
                break;
            }
            case 'A': {
                lRc = saveOrUpdateWOandWHFunc(inTrx, outTrx);//工单领退料
                if (lRc != _NORMAL) {
                    return lRc;
                }
                break;
            }
            default: {
                outTrx.setRtn_mesg("Invalide type input: [" + actionType + "]");
                return E_MWPMTROPE_INVALID_INPUT;
            }
        }
        return lRc;
    }

    private long queryFunc(MwpmtrlopeI inTrx, MwpmtrlopeO outTrx) {
        long lRc = _NORMAL;
        MwpmtrlopeIA iary = inTrx.getIary().get(0);
        String hql ="From Ret_wh_mtrl where 1=1";
        if(!Util.isSpaceCheck(iary.getMtrl_prod_id_fk())){
            hql = hql + " and mtrl_prod_id_fk ='" + iary.getMtrl_prod_id_fk() + "'";
        }
        if(!Util.isSpaceCheck(iary.getDest_shop())){
            hql = hql + " and dest_shop ='" + iary.getDest_shop() + "'";
        }
        List<Ret_wh_mtrl>  ret_wh_mtrls = retWhMtrlDao.find(hql);
        List<MwpmtrlopeOA> oaryList = new ArrayList<>();
        for(Ret_wh_mtrl ret_wh_mtrl : ret_wh_mtrls){
            MwpmtrlopeOA oary = new MwpmtrlopeOA();
            oary.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
            oary.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
            oary.setMtrl_lot_id(ret_wh_mtrl.getMtrl_lot_id());
            Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_wh_mtrl.getMtrl_prod_id_fk());
            if(bis_mtrl!=null){
                oary.setMtrl_name(bis_mtrl.getMtrl_prod_dsc());
            }else{
                oary.setMtrl_name("未知的物料名称");
            }
            oary.setMtrl_qty(ret_wh_mtrl.getMtrl_qty());
            oary.setEvt_qty(ret_wh_mtrl.getEvt_qty());
            oary.setMtrl_wh_qty(ret_wh_mtrl.getMtrl_qty()-ret_wh_mtrl.getEvt_qty());
            String destShop = ret_wh_mtrl.getDest_shop();
            oary.setDest_shop(destShop);
            oaryList.add(oary);
        }
        outTrx.setOary(oaryList);
        return lRc;
    }

    private long saveFunc(MwpmtrlopeI inTrx, MwpmtrlopeO outTrx) {
        long lRc = _NORMAL;
        Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
        List<MwpmtrlopeIA> iaryList = inTrx.getIary();
        for (MwpmtrlopeIA iary : iaryList){
            //获取并检查物料信息
            Bis_mtrl bis_mtrl = bisMtrlDao.get(iary.getMtrl_prod_id_fk());
            if(bis_mtrl == null){
                outTrx.setRtn_mesg("未找到物料代码[" + iary.getMtrl_prod_id_fk() + "]的基础信息[BIS_MTRL]");
                return E_BIS_MTRL + E_READ_NOT_FOUND;
            }

            String hql ="FROM Ret_wh_mtrl WHERE dest_shop='" + iary.getDest_shop()
                    + "' AND mtrl_prod_id_fk ='" + iary.getMtrl_prod_id_fk() + "'";
            if(!Util.isSpaceCheck(iary.getMtrl_lot_id())){
                hql = hql + " AND mtrl_lot_id ='" + iary.getMtrl_lot_id() + "'";
            }else{
                hql = hql + " AND mtrl_lot_id is null";
            }
            List<Ret_wh_mtrl>  ret_wh_mtrls = retWhMtrlDao.find(hql);
            Ret_wh_mtrl ret_wh_mtrl = new Ret_wh_mtrl();
            if(ret_wh_mtrls.size()>0){
                ret_wh_mtrl = ret_wh_mtrls.get(0);
                ret_wh_mtrl.setMtrl_qty(ret_wh_mtrl.getMtrl_qty() + Double.valueOf(iary.getMtrl_qty()));
                ret_wh_mtrl.setEvt_cate(EVT_TOOL_SPARE_WH_U);
                ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
                ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
            }else{
                ret_wh_mtrl.setMtrl_seq_id(GUIDGenerator.javaGUID());
                ret_wh_mtrl.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
                ret_wh_mtrl.setMtrl_lot_id(iary.getMtrl_lot_id());
                ret_wh_mtrl.setMtrl_cate(bis_mtrl.getMtrl_cate());
                ret_wh_mtrl.setMtrl_cus_id_fk(bis_mtrl.getMtrl_mkr());
                ret_wh_mtrl.setInwh_timestamp(gEvtTimestamp);
                ret_wh_mtrl.setMtrl_qty(Double.valueOf(iary.getMtrl_qty()));
                ret_wh_mtrl.setEvt_qty(Double.valueOf(0));
                ret_wh_mtrl.setDest_shop(iary.getDest_shop());
                ret_wh_mtrl.setEvt_cate(EVT_TOOL_SPARE_WH);
                ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
                ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
            }
            retWhMtrlDao.saveOrUpdate(ret_wh_mtrl);
        }
        return lRc;
    }

    private long delFunc(MwpmtrlopeI inTrx, MwpmtrlopeO outTrx) {
        long lRc = _NORMAL;
        Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
        List<MwpmtrlopeIA> iaryList = inTrx.getIary();
        List<Ret_wh_rsv> ret_wh_rsvList = new ArrayList<Ret_wh_rsv>();
        List<ERSVXFR> ersvxfrList = new ArrayList<ERSVXFR>();
        for (MwpmtrlopeIA iary:iaryList){
            String hql ="FROM Ret_wh_mtrl WHERE dest_shop='" + iary.getDest_shop()
                    + "' AND mtrl_prod_id_fk ='" + iary.getMtrl_prod_id_fk() + "'";
            if(!Util.isSpaceCheck(iary.getMtrl_lot_id())){
                hql = hql + " AND mtrl_lot_id ='" + iary.getMtrl_lot_id() + "'";
            }else{
                hql = hql + " AND mtrl_lot_id is null";
            }
            List<Ret_wh_mtrl>  ret_wh_mtrls = retWhMtrlDao.find(hql);
            if(ret_wh_mtrls.isEmpty()){
                outTrx.setRtn_mesg("未找到物料代码[" + iary.getMtrl_prod_id_fk() + "]的库存信息");
                return E_RET_WH_MTRL + E_READ_NOT_FOUND;
            }else if(ret_wh_mtrls.size() > 1){
                outTrx.setRtn_mesg("物料代码[" + iary.getMtrl_prod_id_fk() + "]的库存信息不止一条，请确认");
                return E_MWPMTROPE_INVALID_WH_INFO;
            }

            Ret_wh_mtrl ret_wh_mtrl  = ret_wh_mtrls.get(0);
            if(Double.valueOf(iary.getMtrl_qty()) >
                    ret_wh_mtrl.getMtrl_qty() - ret_wh_mtrl.getEvt_qty()){//删除数量不能超过库存剩余数量
                outTrx.setRtn_mesg("删除数量[" + iary.getMtrl_qty()
                        + "]不能超过库存数量[" + (ret_wh_mtrl.getMtrl_qty() - ret_wh_mtrl.getEvt_qty()) + "]");
                return E_MWPMTROPE_INVALID_MTRL_QTY;
            }
            ret_wh_mtrl.setMtrl_qty(ret_wh_mtrl.getMtrl_qty() - Double.valueOf(iary.getMtrl_qty()));
            ret_wh_mtrl.setEvt_cate(EVT_TOOL_SPARE_WH_U);
            ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
            ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
            retWhMtrlDao.update(ret_wh_mtrl);
//
//            //先把信息保存到预留表中，然后把信息传给SAP，完成线边仓到仓库退料操作
//            String  mesRsvNo = "M"+ GUIDGenerator.javaGUID().substring(0,11);
//
//            String sqlN ="select * from bis_data where data_cate='DEST'  and data_ext='" +ret_wh_mtrl.getDest_shop()+"'";
//            List<Bis_data> list = bisDataDao.findBySQL(sqlN,Bis_data.class);
//            if(list==null || list.size()==0){
//                outTrx.setRtn_mesg("[" + ret_wh_mtrl.getDest_shop() + "]的仓库信息不存在");
//                return E_BIS_DATA + E_ADD_EXIST ;
//            }
//            Ret_wh_rsv ret_wh_rsv =new Ret_wh_rsv();
//            Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_wh_mtrl.getMtrl_prod_id_fk());
//            if(bis_mtrl == null){
//                outTrx.setRtn_mesg("未找到物料代码[" + ret_wh_rsv.getMtrl_prod_id_fk() + "]的基础信息[BIS_MTRL]");
//                return E_BIS_MTRL + E_READ_NOT_FOUND;
//            }
//            ret_wh_rsv.setMtrl_seq_id(GUIDGenerator.javaGUID());
//            ret_wh_rsv.setRsv_no(mesRsvNo);
//            ret_wh_rsv.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
//            ret_wh_rsv.setMtrl_cate(bis_mtrl.getMtrl_cate());
//            ret_wh_rsv.setMtrl_pln_qty(Double.valueOf(iary.getMtrl_qty()));
//            ret_wh_rsv.setMtrl_act_qty(0.0);
//            if(!Util.isSpaceCheck(iary.getMtrl_lot_id())){
//                ret_wh_rsv.setMtrl_lot_id(iary.getMtrl_lot_id());
//            }
//            ret_wh_rsv.setSource_shop(ret_wh_mtrl.getDest_shop());
//            ret_wh_rsv.setDest_shop(iary.getFinish_shop());//原材料仓库
//            ret_wh_rsv.setFab_id_fk(list.get(0).getExt_1());
//            ret_wh_rsv.setMove_typ("Z04");
//            ret_wh_rsv.setEvt_usr(inTrx.getEvt_usr());
//            ret_wh_rsv.setEvt_cate(EVT_MES_RSV_IN);
//            ret_wh_rsv.setEvt_timestamp(gEvtTimestamp);
//            ret_wh_rsvList.add(ret_wh_rsv );
//
//
//            //ESB的消息体
//            ERSVXFR ersvxfr = new ERSVXFR();
//            Format dateFormat = new SimpleDateFormat("yyyyMMdd");
//            String date  = dateFormat.format(new Date());
//            ersvxfr.setBDTER(date);
//            ersvxfr.setBWART("Z04");
//            ersvxfr.setWEMPF(mesRsvNo);
//            ersvxfr.setWERKS(ret_wh_rsv.getFab_id_fk());
//            ersvxfr.setMATNR(ret_wh_rsv.getMtrl_prod_id_fk());
//            ersvxfr.setBDMNG(ret_wh_rsv.getMtrl_pln_qty());
//            ersvxfr.setCHARG(ret_wh_rsv.getMtrl_lot_id());
//            ersvxfr.setLGORT(ret_wh_rsv.getDest_shop());
//            ersvxfr.setUMLGO(ret_wh_rsv.getSource_shop());
//            ersvxfrList.add(ersvxfr);
        }
//        try {
//            baseConnFactory.addSAPREQProc("ERSVXFR", JacksonUtil.toJSONStr(ersvxfrList));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        retWhRsvDao.saveOrUpdateAll(ret_wh_rsvList);
        return lRc;
    }


    private long saveOrUpdateWOandWHFunc(MwpmtrlopeI inTrx, MwpmtrlopeO outTrx) {
        long lRc = _NORMAL;
        //检查数据
        if(Util.isSpaceCheck(inTrx.getWh_id_fk())){
            outTrx.setRtn_mesg("仓位代码不能为空");
            return E_MWPMTROPE_INVALID_INPUT;
        }
        if(Util.isSpaceCheck(inTrx.getAction_type())){
            outTrx.setRtn_mesg("操作类型不能为空");
            return E_MWPMTROPE_INVALID_INPUT;
        }
        if(Util.isSpaceCheck(inTrx.getOperation_type())){
            outTrx.setRtn_mesg("业务类型不能为空");
            return E_MWPMTROPE_INVALID_INPUT;
        }
        if(Util.isSpaceCheck(inTrx.getLine_id())){
            outTrx.setRtn_mesg("线别代码不能为空");
            return E_MWPMTROPE_INVALID_INPUT;
        }
        if(Util.isSpaceCheck(inTrx.getWo_id_fk())){
            outTrx.setRtn_mesg("工单代码不能为空");
            return E_MWPMTROPE_INVALID_INPUT;
        }

        List<EWOMTRL> ewomtrlList = new ArrayList<EWOMTRL>();
        for(MwpmtrlopeIA iary:inTrx.getIary()){
            EWOMTRL ewomtrl = new EWOMTRL();
            //获取工单、物料发料信息
            String hql = "FROM Ret_wo_mtrl WHERE mtrl_prod_id_fk ='" + iary.getMtrl_prod_id_fk()
                    + "' AND wo_id_fk='" + inTrx.getWo_id_fk()
                    + "' AND mtrl_cate='" + iary.getMtrl_cate()
                    + "' AND line_id_fk='" + inTrx.getLine_id() + "'";
            if(!Util.isSpaceCheck(iary.getMtrl_lot_id())){
                hql = hql + " AND mtrl_lot_id ='" + iary.getMtrl_lot_id() + "'";
            }else{
                hql = hql + " AND mtrl_lot_id is null";
            }
            List<Ret_wo_mtrl> ret_wo_mtrls = retWoMtrlDao.find(hql);
            if(ret_wo_mtrls.size()>0){
                Ret_wo_mtrl ret_wo_mtrl = ret_wo_mtrls.get(0);
                if("261".equals(inTrx.getAction_type())||"Z15".equals(inTrx.getAction_type())) {//工单领料操作
                    ret_wo_mtrl.setMtrl_qty(ret_wo_mtrl.getMtrl_qty() + Double.valueOf(iary.getMtrl_qty()));
                    ret_wo_mtrl.setEvt_cate(EVT_WO_PICKING);
                }else if("262".equals(inTrx.getAction_type())||"Z14".equals(inTrx.getAction_type())){//工单退料操作
                    if(Double.valueOf(iary.getMtrl_qty()) > ret_wo_mtrl.getMtrl_qty()){
                        outTrx.setRtn_mesg("退料数量[" + Double.valueOf(iary.getMtrl_qty())
                                + "]不能超过工单剩余数量[" + ret_wo_mtrl.getMtrl_qty() + "]");
                        return E_MWPMTROPE_NOT_QTY;
                    }
                    ret_wo_mtrl.setMtrl_qty(ret_wo_mtrl.getMtrl_qty() - Double.valueOf(iary.getMtrl_qty()));
                    ret_wo_mtrl.setEvt_cate(EVT_WO_RETURN);
                }else{
                    outTrx.setRtn_mesg("操作类型必须为261,262,Z15,Z16");
                    return E_MWPMTROPE_INVALID_INPUT;
                }
                ret_wo_mtrl.setEvt_qty(Double.valueOf(iary.getMtrl_qty()));
                ret_wo_mtrl.setEvt_usr(inTrx.getEvt_usr());
                ret_wo_mtrl.setEvt_timestamp(Util.getCurrentTimestamp());
                retWoMtrlDao.update(ret_wo_mtrl);
            }else{
                if("261".equals(inTrx.getAction_type())||"Z15".equals(inTrx.getAction_type())) {//工单领料操作
                    Ret_wo_mtrl ret_wo_mtrl = new Ret_wo_mtrl();
                    ret_wo_mtrl.setMtrl_seq_id(GUIDGenerator.javaGUID());
                    ret_wo_mtrl.setWo_id_fk(inTrx.getWo_id_fk());
                    ret_wo_mtrl.setMtrl_cate(iary.getMtrl_cate());
                    ret_wo_mtrl.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
                    ret_wo_mtrl.setLine_id_fk(inTrx.getLine_id());
                    ret_wo_mtrl.setMtrl_lot_id(iary.getMtrl_lot_id());
                    ret_wo_mtrl.setMtrl_qty(Double.valueOf(iary.getMtrl_qty()));
                    ret_wo_mtrl.setEvt_qty(Double.valueOf(iary.getMtrl_qty()));
                    ret_wo_mtrl.setEvt_cate(EVT_WO_PICKING);
                    ret_wo_mtrl.setEvt_usr(inTrx.getEvt_usr());
                    ret_wo_mtrl.setEvt_timestamp(Util.getCurrentTimestamp());
                    ret_wo_mtrl.setExt_2(inTrx.getOperation_type());
                    retWoMtrlDao.save(ret_wo_mtrl);
                }else if("262".equals(inTrx.getAction_type())||"Z14".equals(inTrx.getAction_type())){//工单退料操作
                    outTrx.setRtn_mesg("工单[" + inTrx.getWo_id_fk()
                            + "]未领取物料种类[" + iary.getMtrl_cate()
                            + "],物料代码[" + iary.getMtrl_prod_id_fk()
                            + "],批次代码[" + iary.getMtrl_lot_id()
                            + "]的物料，无法退料");
                    return E_MWPMTROPE_WO_MTRL_NOT_FOUND;
                }else{
                    outTrx.setRtn_mesg("操作类型必须为261或者262");
                    return E_MWPMTROPE_INVALID_INPUT;
                }
            }

            //获取物料库存信息，并扣减物料库存信息
            String hqlM = "FROM Ret_wh_mtrl WHERE dest_shop='" + inTrx.getWh_id_fk()
                    + "' AND mtrl_cate='" + iary.getMtrl_cate()
                    + "' AND mtrl_prod_id_fk='" + iary.getMtrl_prod_id_fk() + "'";
            if(!Util.isSpaceCheck(iary.getMtrl_lot_id())){
                hqlM = hqlM + " and mtrl_lot_id ='" + iary.getMtrl_lot_id() + "'";
            }else{
                hqlM = hqlM + " AND mtrl_lot_id is null";
            }
            List<Ret_wh_mtrl> ret_wh_mtrls = retWhMtrlDao.find(hqlM);
            if (ret_wh_mtrls.size()>0){
                Ret_wh_mtrl ret_wh_mtrl = ret_wh_mtrls.get(0);
                if("261".equals(inTrx.getAction_type())||"Z15".equals(inTrx.getAction_type())) {//工单领料操作
                    if(Double.valueOf(iary.getMtrl_qty()) >
                            ret_wh_mtrl.getMtrl_qty() - ret_wh_mtrl.getEvt_qty()){//领料数量不能超过库存剩余数量
                        outTrx.setRtn_mesg("领料数量[" + iary.getMtrl_qty()
                                + "]不能超过库存数量[" + (ret_wh_mtrl.getMtrl_qty() - ret_wh_mtrl.getEvt_qty()) + "]");
                        return E_MWPMTROPE_INVALID_MTRL_QTY;
                    }
                    ret_wh_mtrl.setEvt_qty(ret_wh_mtrl.getEvt_qty() + Double.valueOf(iary.getMtrl_qty()));
                    ret_wh_mtrl.setEvt_cate(EVT_WO_PICKING);
                }else if("262".equals(inTrx.getAction_type())||"Z14".equals(inTrx.getAction_type())){//工单退料操作
                    if(Double.valueOf(iary.getMtrl_qty()) > ret_wh_mtrl.getEvt_qty()){
                        outTrx.setRtn_mesg("退料数量[" + iary.getMtrl_qty()
                                + "]不能超过领料数量[" + ret_wh_mtrl.getEvt_qty() + "]");
                        return E_MWPMTROPE_INVALID_MTRL_QTY;
                    }
                    ret_wh_mtrl.setEvt_qty(ret_wh_mtrl.getEvt_qty() - Double.valueOf(iary.getMtrl_qty()));
                    ret_wh_mtrl.setEvt_cate(EVT_WO_RETURN);
                }else{
                    outTrx.setRtn_mesg("操作类型必须为261,262,Z14,Z15");
                    return E_MWPMTROPE_INVALID_INPUT;
                }
                ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
                ret_wh_mtrl.setEvt_timestamp(Util.getCurrentTimestamp());
                retWhMtrlDao.update(ret_wh_mtrl);
            }else{//没有库存信息,则新增。
               /* outTrx.setRtn_mesg("仓库代码[" + inTrx.getWh_id_fk()+ "]不存在物料种类[" + iary.getMtrl_cate()+ "],物料代码[" + iary.getMtrl_prod_id_fk()+ "],批次代码[" + iary.getMtrl_lot_id() + "]的库存信息");
                return E_MWPMTROPE_MTRL_WH_NOT_FOUND;*/
               Ret_wh_mtrl ret_wh_mtrl = new Ret_wh_mtrl();
                ret_wh_mtrl.setMtrl_seq_id(GUIDGenerator.javaGUID());
                ret_wh_mtrl.setUnq_seq_id(GUIDGenerator.javaGUID());
                ret_wh_mtrl.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
                ret_wh_mtrl.setMtrl_cate(iary.getMtrl_cate());
                ret_wh_mtrl.setMtrl_lot_id(iary.getMtrl_lot_id());
                ret_wh_mtrl.setDest_shop(inTrx.getWh_id_fk());
                ret_wh_mtrl.setInwh_timestamp(Util.getCurrentTimestamp());
                ret_wh_mtrl.setMtrl_qty(Double.valueOf(iary.getMtrl_qty()));
                ret_wh_mtrl.setEvt_qty(0.0);
                ret_wh_mtrl.setEvt_cate(EVT_WO_RETURN);
                ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
                ret_wh_mtrl.setEvt_timestamp(Util.getCurrentTimestamp());
                retWhMtrlDao.save(ret_wh_mtrl);
            }
            //领退料消息返回SPC
            Ret_wo_bomId  ret_wo_bomId = new Ret_wo_bomId();
            ret_wo_bomId.setWo_id_fk(inTrx.getWo_id_fk());
            ret_wo_bomId.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
            ret_wo_bomId.setMtrl_cate(iary.getMtrl_cate());
            Ret_wo_bom ret_wo_bom = retWoBomDao.get(ret_wo_bomId);
            ewomtrl.setAUFNR(inTrx.getWo_id_fk());
            ewomtrl.setRSNUM(ret_wo_bom.getRsv_no());
            ewomtrl.setRSPOS(ret_wo_bom.getItem_no());
            ewomtrl.setMATNR(iary.getMtrl_prod_id_fk());
            ewomtrl.setMENGE(Double.valueOf(iary.getMtrl_qty()));
            ewomtrl.setMEINS(ret_wo_bom.getMtrl_unit_fk());
            ewomtrl.setCHARG(iary.getMtrl_lot_id());
            ewomtrl.setBWART(inTrx.getAction_type());
            ewomtrl.setZTYPE(inTrx.getOperation_type());
            ewomtrl.setWERKS(ret_wo_bom.getFab_id_fk());
            ewomtrl.setLGORT(inTrx.getWh_id_fk());
            Format dateFormat = new SimpleDateFormat("yyyyMMdd");
            String date  = dateFormat.format(new Date());
            ewomtrl.setBUDAT(date);
            ewomtrlList.add(ewomtrl);
        }
       /* try {
            baseConnFactory.addSAPREQProc("EWOMTRL", JacksonUtil.toJSONStr(ewomtrlList));
        }catch (Exception e){
            e.printStackTrace();
        }*/
        return lRc;
    }
}

